package org.tong.common.utils;

import lombok.Data;
import org.tong.common.exception.BaseExceptionInterface;
import org.tong.common.exception.BizException;

import java.io.Serializable;

@Data
public class Response<T> implements Serializable {
    private boolean success = true;
    private String message;
    private String errorCode;
    private T data;

    public static <T> Response<T> success(){
        Response<T> response = new Response<>();
        response.setMessage(null);
        response.setData(null);
        response.setErrorCode(null);
        return response;
    }

    public static <T> Response<T> success(T data){
        Response<T> response = new Response<>();
        response.setSuccess(true);
        response.setMessage(null);
        response.setData(data);
        response.setErrorCode(null);
        return response;
    }

    public static <T> Response<T> fail(){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(null);
        response.setData(null);
        response.setErrorCode(null);
        return response;
    }

    public static <T> Response<T> fail(String message){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(message);
        response.setData(null);
        response.setErrorCode(null);
        return response;
    }

    public static <T> Response<T> fail(String errorCode,String message){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(message);
        response.setErrorCode(errorCode);
        response.setData(null);
        return response;
    }

    public static <T> Response<T> fail(BizException bizException){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(bizException.getErrorMessage());
        response.setErrorCode(bizException.getErrorCode());
        return response;
    }

    public static <T> Response<T> fail(BaseExceptionInterface baseExceptionInterface){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(baseExceptionInterface.getErrorMessage());
        response.setErrorCode(baseExceptionInterface.getErrorCode());
        return response;
    }
}
